package com.stox.core.model;

import java.util.Calendar;
import java.util.Date;

import com.stox.core.model.exception.StoxException;
import com.stox.core.model.intf.HasBars;
import com.stox.core.util.StringUtil;
import com.stox.resources.i18n.MessageSource;

public class Scrip implements Comparable<Scrip>, HasBars{

	private String id;
	private String code;
	private String name;
	private String exchangeCode;
	private String type;
	private String group;
	
	private Date minDate;
	private Date maxDate;
	
	public void validate(){
		if(!StringUtil.hasText(getCode())){
			throw new StoxException(MessageSource.getMessage("Scrip.code.is.null.or.empty"));
		}
		if(!StringUtil.hasText(getName())){
			throw new StoxException(MessageSource.getMessage("Scrip.name.is.null.or.empty"));
		}
		if(!StringUtil.hasText(getExchangeCode())){
			throw new StoxException(MessageSource.getMessage("Exchange.code.for.scrip.is.null.or.empty"));
		}
	}
	
	public void format(){
		validate();
		setCode(getCode().trim().toUpperCase());
		setName(getName().trim());
		setExchangeCode(getExchangeCode().trim().toUpperCase());
		if(getId() != null){
			setId(getId().trim().toUpperCase());
		}
	}
	
	public int compareTo(Scrip o) {
		if(o != null && o.getName() != null && getName() != null){
			return getName().compareToIgnoreCase(o.getName());
		}
		return 0;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getExchangeCode() {
		return exchangeCode;
	}

	public void setExchangeCode(String exchangeCode) {
		this.exchangeCode = exchangeCode;
	}

	

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result
				+ ((exchangeCode == null) ? 0 : exchangeCode.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Scrip other = (Scrip) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (exchangeCode == null) {
			if (other.exchangeCode != null)
				return false;
		} else if (!exchangeCode.equals(other.exchangeCode))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return getName();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Date getMinDate() {
		if(minDate == null){
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.YEAR, -3);
			minDate = calendar.getTime();
		}
		return minDate;
	}

	public void setMinDate(Date minDate) {
		this.minDate = minDate;
	}

	public Date getMaxDate() {
		if(maxDate == null){
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.YEAR, -3);
			maxDate = calendar.getTime();
		}
		return maxDate;
	}

	public void setMaxDate(Date maxDate) {
		this.maxDate = maxDate;
	}
	
}
